using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Practices.Unity.Interception
{
    [TestClass]
    public class InterceptInterfacePropertiesInterceptionExtensionTest
    {
        [TestMethod]
        public void CanInterceptPropertySettersAndGetters()
        {
            Recorder.Records.Clear();
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<InterceptionExtension>();
            container.RegisterType<IFoo, Foo>();

            IFoo obj = container.Resolve<IFoo>();
            obj.MyProperty = "foo";
            string unused = obj.MyProperty;

            Assert.AreEqual(6, Recorder.Records.Count);
            Assert.AreEqual("Before Method", Recorder.Records[0]);
            Assert.AreEqual("PropSet: foo", Recorder.Records[1]);
            Assert.AreEqual("After Method", Recorder.Records[2]);
            Assert.AreEqual("Before Method", Recorder.Records[3]);
            Assert.AreEqual("PropGet", Recorder.Records[4]);
            Assert.AreEqual("After Method", Recorder.Records[5]);
        }

        public class Foo : IFoo
        {
            public string MyProperty
            {
                [InterfaceIntercept(typeof(RecordingHandler))]
                get
                {
                    Recorder.Records.Add("PropGet");
                    return null;
                }

                [InterfaceIntercept(typeof(RecordingHandler))]
                set { Recorder.Records.Add("PropSet: " + value); }
            }
        }

        public interface IFoo
        {
            string MyProperty { get; set; }
        }
    }
}